<!DOCTYPE html>

<style type="text/css">
.text-red {
	color: red;
	font-weight: 600;
	font-size: 18px;
	position: relative;
	top: 3px;
	margin-right: 3px;
	margin-left: 0px;
	padding: 0px;
}
</style>

<script type="text/javascript">
$(function(){
	$('#year').selectpicker({	//初始化下拉框
		title:'年级',
		width:'fit'
	}); 
	$('#college').selectpicker({	//初始化下拉框
		title:'专业',
		width:'fit'
	}); 
	$('#major').selectpicker({	//初始化下拉框
		title:'班级',
		width:'fit'
	}); 
	$('#schoolyear').selectpicker({	//初始化下拉框
		title:'学年学期',
		width:'fit'	
	}); 
	$('#search_teacher').selectpicker({	//初始化下拉框
		title:'教师',
		width:'fit'	
	}); 
	
	$('#schoolyearone').selectpicker({	//初始化下拉框
		title:'学年学期'
	}); 
	$('#schedule').selectpicker({	//初始化下拉框
		title:'选择课表安排的时间段'
	}); 
	/* 往自动生成课表里增加选择的学年学期 */
	$.post('classSchedule/findAgcsSchoolyear',function(data){
		for (var i = 0; i < data.length; i++) {  
		$('#schoolyearone').append("<option value=" + data[i].id + ">"+data[i].xnxq +"</option>");  
		}
		$('#schoolyearone').selectpicker('refresh');  
	});
});

	

/* 当年级发生改变时 */
function year(value){
	$("#schoolyear").find("option").remove();
	$('#schoolyear').selectpicker('refresh');
	$.ajax({  
		// get请求地址  
		    url: 'classSchedule/yearChanges?year='+value,  
		    dataType: "json",  
		    success: function (data) {
		    	$('#college,#major').html(
				   '<option style="display: none"></option>');
		        for (var i = 0; i < data.length; i++) {  
		            $('#college').append("<option value=" + data[i].profesion + "> "+data[i].profesion +"</option>");  
		        }  
		        // 缺一不可  
		        $('#college').selectpicker('refresh');  
		        $('#major').selectpicker('refresh'); 
		    }  
		});  
	
	//加载学年学期框内的数据（显示被添加过的所有学年学期,与其余菜单无联动）
	$.post('classSchedule/findSchoolYear',{year:value},function(data){
		$('#schoolyear').html('<option style="display: none"></option>');
		for (var i = 0; i < data.length; i++) {  
		$('#schoolyear').append("<option value=" + data[i].xnxq + ">"+data[i].xnxq +"</option>");  
		}
		 // 缺一不可  
    	$('#schoolyear').selectpicker('refresh');  
		});
}
/* 当专业发生改变时 */
function collegeChange(college){
	var year=$('#year').val();
	$.ajax({  
		// get请求地址  
		    url: 'classSchedule/majorChange',
		    data:{major:college,years:year},
		    dataType: "json",  
		    success: function (data) {  
		    	$('#major').html(
				'<option style="display: none"></option>');
		        for (var i = 0; i < data.length; i++) {  
		            $('#major').append("<option value=" +data[i].name + "> "+data[i].name +"</option>");  
		        }  
		        // 缺一不可  
		        $('#major').selectpicker('refresh');  
		    }  
		});  
}

function xnxq(){
	$.post('classSchedule/findAgcsSchoolyear',function(data){
		$('#schoolyear').html('<option style="display: none"></option>');
		for (var i = 0; i < data.length; i++) {  
		$('#schoolyear').append("<option value=" + data[i].xnxq + ">"+data[i].xnxq +"</option>");  
		}
		 // 缺一不可  
    	$('#schoolyear').selectpicker('refresh');  
		});
}
$(function(){
	$.ajax({
		url:'classSchedule/getTeacher',
		dataType:'json',
		type:'post',
		success:function(result){
			if(result!=null&&result.length>0){
				 for (var i = 0; i < result.length; i++) {  
			            $('#search_teacher').append("<option value=" +result[i].name + "> "+result[i].name +"</option>");  
			        }
			}
			$('#search_teacher').selectpicker('refresh');  
		}
	});
	
});
</script>

<div id="classSchedule_table_div">
	<!-- 查询窗口 -->
	<div class="modal fade" id="myModalkc" tabindex="-1" role="dialog"
		aria-labelledby="myModalLabel">
		<div class="modal-dialog" role="document">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal"
						aria-label="Close">
						<span aria-hidden="true">&times;</span>
					</button>
					<h4 class="modal-title" id="myModalLabel">查询</h4>
				</div>
				<div class="modal-body">
					<!-- 以下为要查询的的内容 -->
					<div class="text-center">
						<select id="year" name="year" class="selectpicker"
							onchange="year(this.value)" data-bind="value:queryCondition.year">
							<option style="display: none" value=""></option>
							<option th:each="y:${year}" th:text="${y}"></option>
						</select>&nbsp;&nbsp; <select id="college" class="selectpicker"
							name="college" data-bind="value:queryCondition.college"
							data-max-options="1" onchange="collegeChange(this.value)">
							<option style="display: none" value=""></option>
						</select>&nbsp;&nbsp; <select id="major" class="selectpicker" name="major"
							data-bind="value:queryCondition.major">
							<option style="display: none" value=""></option>
						</select>&nbsp;&nbsp; <select id="schoolyear" class="selectpicker"
							name="schoolyear" data-bind="value:queryCondition.schoolyear">
							<option style="display: none" value=""></option>
						</select>&nbsp;&nbsp; <select id="search_teacher" class="selectpicker"
							name="searchTeacher" onchange="xnxq()"
							data-bind="value:queryCondition.searchTeacher" ><option
								style="display: none"></option></select>
					</div>
					<!-- 以上为要查询的内容 -->
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-primary" data-dismiss="modal"
						data-bind="click:queryClick">
						<span class="glyphicon glyphicon-search" aria-hidden="true"></span>&nbsp;查询
					</button>
					<button type="button" class="btn btn-default" data-dismiss="modal"
						data-bind="click:clearClick" onclick="cleanSearch()">
						<span class="glyphicon glyphicon-trash" aria-hidden="true"></span>&nbsp;清空
					</button>
				</div>
			</div>

		</div>
	</div>



	<!-- 工具栏 -->
	<div id="toolbar" class="btn-group">
		<button  type="button" class="btn btn-default" data-toggle="modal" data-target="#myagcs">
			<span class="glyphicon glyphicon-hourglass" aria-hidden="true"></span>自动生成课表
		</button>
		<button data-bind="click:addClick" type="button"
			class="btn btn-default">
			<span class="glyphicon glyphicon-plus" aria-hidden="true"></span>新增
		</button>
		<button data-bind="click:editClick" type="button"
			class="btn btn-default">
			<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>修改
		</button>
		<button data-bind="click:deleteClick" type="button"
			class="btn btn-default">
			<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>删除
		</button>
		<button  data-bind="click:searchClick" type="button"
			class="btn btn-default">
			<span class="glyphicon glyphicon-search" aria-hidden="true"></span>
			<span id="tit">查询</span>
		</button>

		<button type="button" class="btn btn-default"
			onclick="window.location.href='classSchedule/export'">
			<span class="glyphicon glyphicon-paste"></span> 导出
		</button>
		<button type="button" class="btn btn-warning" data-toggle="modal"
			data-target="#generate-modal">
			<span class="glyphicon glyphicon-folder-close" aria-hidden="true"></span>
			生成授课记录
		</button>
	</div>


	<!-- 表格，表头里定义了表要显示的字段，data-formatter 里只能跟一个方法名 -->
	<table id="mytable" data-bind="bootstrapTable:$root">
		<thead>
			<tr>
				<th data-checkbox="true"></th>
				<th data-field="kcmc">课程</th>

				<th data-field="tbclassName">班级</th>
				<th data-field="teacherName">教师</th>
				<th data-field="zc">周次</th>
				<th data-field="xq">星期</th>
				<th data-field="jcsj">节次</th>
				<th data-field="dd">地点</th>
				<th data-field="xf" data-visible="false">学分</th>
				<th data-field="zongxs" data-visible="false">总学时</th>
				<th data-field="khfs" data-visible="false">考核方式</th>
				<th data-field="tbclassNumber">上课人数</th>
				<th data-field="year">年级</th>
				<th data-field="profession">专业</th>
			</tr>
		</thead>
	</table>
</div>

<div id="generate-modal" class="modal">
	<div class="modal-dialog modal-md">
		<div class="modal-content">
			<div class="modal-header">
				<button class="close" data-dismiss="modal">
					<span>&times;</span>
				</button>
				<h4>选择操作</h4>

			</div>
			<div class="modal-body">
				<form id="generateForm" action="">
					<div class="form-group row">
						<div class="col-sm-4 col-md-offset-1">
							<input id="startTime" name="startTime" class="form-control"
								type="text" placeholder="开始时间" readonly>
						</div>
						<div class="col-sm-2 text-center">
							<label style="position: relative; top: 5px;">---</label>
						</div>
						<div class="col-sm-4">
							<input id="endTime" name="endTime" class="form-control"
								type="text" placeholder="结束时间" readonly>
						</div>
					</div>
				</form>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-info"
					onclick="generateAllTab()">生成当前季度</button>
				<button type="button" class="btn btn-info" onclick="generateUp()">更新授课记录</button>
				<button type="button" data-dismiss="modal" class="btn btn-default">
					<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>关闭
				</button>
			</div>
		</div>
	</div>
</div>


<div id="progress-modal" class="modal">
	<div class="modal-dialog modal-md">
		<div class="modal-content">
			<div class="modal-header">
				操作中...
				<!-- <button class="close" data-dismiss="modal"><span>&times;</span></button> -->
			</div>
			<div class="modal-body" id="progress-content">
				<div style="z-index: 100;">
					<div class="progress">
						<div id="progress"
							class="progress-bar  progress-bar-striped active"
							style="width: 40%;">40%</div>
					</div>
				</div>
			</div>
			<div id="foot" class="modal-footer"></div>
		</div>
	</div>
</div>
<!-- 以下为自动生成课表的弹框 -->
<div class="modal fade" id="myagcs" tabindex="-1" role="dialog" aria-labelledby="myModalAgcs">
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalAgcs">自动生成课表</h4>
      </div>
      <div class="modal-body">
      	<div class="row">
      		 <div class="col-md-8 col-md-offset-2">
	      		 <form action="">
		      		 <select id="schoolyearone" name="schoolyearID" class="selectpicker">
						<option style="display: none" value=""></option>
					</select>&emsp;
					
					<select id="schedule" name="schedule" class="selectpicker" >
						<option style="display: none" value=""></option>
						<option value="1">不安排周末、不安排晚自习</option>
						<option value="2">安排周末不、安排晚自习</option>
						<option value="3">不安排周末、安排晚自习</option>
						<option value="4">安排周末、安排晚自习</option>
					</select>&emsp;
	      		 	<button type="button" class="btn btn-info" id="agcsbtn" onclick="myagcs()">生成课表</button>
	      		 </form>
      		 </div>
      			<Br><br><br>
      			
      		 <div class="col-md-12" id="agcsforms" style="text-align: center;">
      		 	
      		 </div>
      		  <div class="col-md-12" id="agcstable" style="text-align: center;">
      		 	
      		 </div>
      	</div>
        	
			
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-primary" style="display:none;">重新生成一次</button>
        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
      </div>
    </div>
  </div>
</div>
<!-- 以下是javascript内容 -->
<script type="text/javascript">
$(function(){
    var data =  {
	    tableParams : {
	    	url : 'classSchedule/page'
	    	},
	    	urls : {
	    		delete : 'classSchedule/delete',
	    		update : 'classSchedule/edit',
	    		add : 'classSchedule/edit',
	    		search:'classSchedule/search'
	    	},
	    	queryCondition : {
	    		year : '',
	    		college : '',
	    		major : '',
	    		schoolyear:'',
	    		searchTeacher:''
	    	}
	};
	ko.applyBindings(new BsTableViewModel(data),document.getElementById('classSchedule_table_div'));	
	
});
function scyearformatter(value,row,index){
	return value==null?'':value.xnxq;
};

$(function(){
	$('#myModalkc').on('hidden.bs.modal', function (e) {
		var searchYear = $('#year').val();
		var searchCollege = $('#college').val();
		var searchMajor = $('#major').val();
		var searchSchoolyear = $('#schoolyear').val();
		var searchTeacher=$('#search_teacher').val();
		var tit='';
		if(searchYear!=null&&searchYear!=''){
			tit+=searchYear+' \\ ';
		}
		if(searchCollege!=null&&searchCollege!=''){
			tit+=searchCollege+' \\ ';
		}
		if(searchMajor!=null&&searchMajor!=''){
			
			tit+=searchMajor+' \\ ';
		}
        if(searchSchoolyear!=null&&searchSchoolyear!=''){
			
			tit+=searchSchoolyear+' \\ ';
		}
        if(searchTeacher!=null&&searchTeacher!=''){
        	tit+=searchTeacher+' \\ ';
        }
		if(tit!=''){
			$('#tit').html(tit.slice(0,-2));
		}else{
			$('#tit').html('查询');
		}
		})
})

/* 根据当前时间获取当前季度 */
function getQuarter(){
	var one=[0,1,2,3,4,5];
	var two=[6,7 ,8,9,10,11];
	var myDate=new Date();
	var year=myDate.getFullYear();
	var month=myDate.getMonth();
	
	for(var i in one){
		if(one[i]==month){
			return year+'春季学期';
		}
	}
	for(var i in two){
		if(two[i]==month){
			return year+'秋季学期';
		}
	}
	return 'error';
};

/* 延时执行 */
function sleep(num){
	var myDate=new Date();
	var exitTime=myDate.getTime()+num;
	
	while(true){
		myDate=new Date();
		if(myDate.getTime()>=exitTime){
			return;
		}
	}
};
function generateAllTab(){
	var quarter=getQuarter();
 	 bootbox.confirm({
    	size:'small',
    	message:'是否要生成'+quarter+'的课表？警告：重复操作会覆盖之前的记录',
    	callback:function(result){
    		if(result){
    			$('#progress-modal').modal({backdrop:false});
    			$.post('classSchedule/generateTab',function(result){
    				if(result=='ok'){
    					$('#generate-modal').modal('hide');
    					$('#progress').css("width","100%");
    					$('#progress').html('100%  成功');
    					var s="<button type='button' class='btn btn-info' data-dismiss='modal'><span class='glyphicon glyphicon-ok' aria-hidden='true'></span>完成</button>";
    					$('#foot').html(s);
    					
    				}else if(result=='error'){
    					 bootbox.alert('未知错误，请重试');
    					 $('#progress-modal').modal('hide');
    				}else{
    					bootbox.alert(result);
    					$('#progress-modal').modal('hide');
    				}
    			});
    		}
    		}
}) 
};

function generateUp(){
	var startTime=$('#startTime').val();
	var endTime=$('#endTime').val();
	if(startTime==''||startTime==null||startTime==undefined){
		bootbox.alert('开始时间未选择！');
		return;
	}
	if(endTime==''||endTime==null||endTime==undefined){
		bootbox.alert('结束时间未选择！');
		return;
	}
	if(startTime.replace('-','')>=endTime.replace('-','')){
		bootbox.alert('开始时间不能大于结束时间！');
		return;
	}
	$.post('classSchedule/generateTab',{startTime:startTime,endTime:endTime},function(result){
		$('#progress-modal').modal({backdrop:false});
		if(result=='ok'){
			$('#generate-modal').modal('hide');
			$('#progress').css("width","100%");
			$('#progress').html('100%  成功');
			var s="<button type='button' class='btn btn-info' data-dismiss='modal'><span class='glyphicon glyphicon-ok' aria-hidden='true'></span>完成</button>";
			$('#foot').html(s);
			
		}else if(result=='error'){
			 bootbox.alert('未知错误，请重试');
			 $('#progress-modal').modal('hide');
		}else{
			bootbox.alert(result);
			 $('#progress-modal').modal('hide');
		}
	});
}

//清空查询中的bootselect下拉框
function cleanSearch(){
	var searchs = $('#myModalkc').find('div.modal-body').find('select.selectpicker');
	for(var i=0;i<searchs.length;i++){
		if(searchs[i].name=='searchTeacher'||searchs[i].name=='year'){
			$(searchs[i]).selectpicker('val','');
			continue;
		}
		$(searchs[i]).html('<option style="display: none"></option>');
		$(searchs[i]).selectpicker('refresh');
	} 
	
};


$(function() {
	laydate.render({
		elem : '#startTime'
	});
	laydate.render({
		elem : '#endTime'
	});
});
//进行生成课表
function myagcs(){
	var schoolyearID=$("#schoolyearone").val();
	var schedule=$("#schedule").val(); 
	if(schoolyearID == "" || schedule == ""){
		bootbox.alert('请选择');
	}else{
		$("#agcsbtn").attr('disabled',true);
		$("#agcsforms").html(' <img src="static/img/loading.gif" width="63%" height="100"/>');
		 
		$.post('agcs/schedule',{schoolyearID:schoolyearID,schedule:schedule},function(data){
			if(data!=null){
				 bootbox.alert('生成成功，表格内内容为未能自动安排课表的课程！');
				 $("#agcsforms").html("");
				 var html='<table class="table table-hover">';
				 html +='<caption>为未能自动安排课表的课程</caption>';
				 html +='<tr>';
				 html +='<th style="text-align: center;">课程名称</th>';
				 html +='<th style="text-align: center;">课程代码</th>';
				 html +='<th style="text-align: center;">任课老师</th>';
				 html +='<th style="text-align: center;">任课班级</th>';
				 html +='<th style="text-align: center;">未生成原因</th>';
				 html +='</tr>';
				 for (var i = 0; i < data.length; i++) { 
					 html +='<tr  style="text-align: center;">';
					 html +='	<td >'+data[i].ed.curriculum.kcmc+'</td>';
					 html +='	<td >'+data[i].ed.curriculum.kcdm+'</td>';
					 html +='	<td >'+data[i].ed.teacher.name+'</td>';
					 html +='	<td >'+data[i].ed.bj+'</td>';
					 html +='	<td >'+data[i].records+'</td>';
					 html +='</tr>';
			     }
				html +='</table>';
				$("#agcstable").html(html);
			}else{
				bootbox.alert('生成成功，课表生成已全部完成');
			}
			$("#mytable").bootstrapTable("refresh", {
			    silent: true //静态刷新 
			  });
		});
	}
}

</script>
